'use strict';
app.controller('goodsTypeCtrl',['$scope','$modal','$log','$state','utilService','toaster','goodsTypeService', function($scope,$modal,$log,$state,utilService ,toaster,goodsTypeService){
    $scope.goodstypes=[];
    $scope.limit=1;
    $scope.imgUrlsQueue = [];
    $scope.imgUrls=new Array();
    $scope.path=$scope.app.image.type;
    $scope.typeImgVO={};

    //查询所有分类
    $scope.findAll=function(){
        $scope.app.loading=true;

        goodsTypeService.findAll($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.goodstypes=result.data;
                    $scope.app.loading=false;
                    break;
                default :
                    $scope.app.loading=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.findAll();

    //添加分类
    $scope.typeAddVO={};
    $scope.addTypes=function(){
        goodsTypeService.addTypes($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findAll();
                    $scope.open();
                    $scope.getAllName();
                    $scope.typeAddVO={};
                    break;
                case 606:
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //获得所有分类名字
    $scope.getAllName=function(){
        goodsTypeService.getAllName($scope).then(function(result){
            var goodstypeNames=[];
            switch (result.status){
                case 200:
                    $scope.goodstypeNames= result.data;
                    break;
                default :
                    break;
            }
        });
    }

    $scope.getAllName();

    //添加子分类
    $scope.addChildType=function(parentId){
        $scope.isOpen=true;
        roundTypes($scope.goodstypes);
        $scope.typeAddVO.parentId=parentId;
    }


    //打开添加分类方框
    $scope.isOpen=false;

    $scope.open=function(){
        roundTypes($scope.goodstypes);
        $scope.isOpen=!$scope.isOpen;
    }
    $scope.cancel=function(){
        $scope.isOpen=false;
    }


    //修改分类
    $scope.editShow= function (types) {
        roundTypes($scope.goodstypes);
        types.editVisiable=!types.editVisiable;
        $scope.typeEditVO.name=types.name;
    }
    $scope.cancelEdit=function(types){
        types.editVisiable=false;
    }

    $scope.typeEditVO={};
    $scope.editTypes= function (type) {
        $scope.typeEditVO.id=type.id;
        if($scope.typeEditVO.name==type.name){
            $scope.cancelEdit(type)
            return;
        }

        goodsTypeService.editTypes($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findAll();
                    $scope.getAllName();
                    $scope.cancelEdit(type);
                    $scope.typeEditVO={};
                    break;
                case 606:
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //类型图片
    $scope.init=function(type){
        type.uploadVisiable=false;
        type.visiable=false;
        type.editVisiable=false;
    }

    var roundTypes=function(types){
        for(var i=0;i<types.length;i++){
            types[i].uploadVisiable=false;
            types[i].editVisiable=false;
            if(types[i].children.length>0){
                roundTypes(types[i].children);
            }
        }
    }

    //删除分类
    $scope.deleteType = function(id, name){
        roundTypes($scope.goodstypes);

        var param = {
            id:id,
            name:name
        };

        // query data.
        openModal("你是否要删除【" + name + "】分类以及其子分类？");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除分类",
                controller:"deleteTypeCtrl",
                message:msg,
                data:param
            });
        }
    }

    $scope.showPicBox=function(type){
        $scope.imgUrlsQueue=[];
        roundTypes($scope.goodstypes);
        $scope.uploadState=true;
        $scope.typeImgVO.id=type.id;
        $scope.findbyid();
    }

    $scope.findbyid= function () {
        $scope.imgUrls=[];
        goodsTypeService.findbyid($scope).then(function(result){
            switch (result.status){
                case 200:
                    var data=result.data;
                    var imgUrl=data.imgUrl;
                    if(imgUrl==null || angular.isUndefined(imgUrl) || imgUrl==''){
                        $scope.limit=1;
                    }else{
                        $scope.limit=0;
                        $scope.imgUrls.push(imgUrl);
                    }

                    break;
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.cancelUploadPic=function(){
        $scope.uploadState=false;
    }

    $scope.saveImg=function(){
        if($scope.imgUrlsQueue.length>0){
            $scope.typeImgVO.imgUrl=$scope.imgUrlsQueue[0].fileName;
        }

        goodsTypeService.saveImg($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.reload('app.goodsTypes');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

    }

    $scope.delImg=function(){
        goodsTypeService.delImg($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findbyid();
                    break;
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

}]);

app.controller('deleteTypeCtrl', ['$scope', '$modalInstance','$state','goodsTypeService','toaster','modalData',
    function($scope, $modalInstance,$state,goodsTypeService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            goodsTypeService.deleteType($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $modalInstance.dismiss('success');
                        $scope.findAll();
                        break;
                    default :
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);










